#include "gamestate.h"

#include <game/game.h>
#include <game/keymanager.h>

#include <gui/gamescene.h>
#include <game/gameserver.h>
#include <gui/statemanager.h>

GameState::GameState(StateManager *stateManager, KeyManager *keyManager, QObject *parent) : State(parent)
{
    myKeyManager = keyManager;
    myStateManager = stateManager;

    myGame = new Game(myKeyManager, this);
    connect(myGame, SIGNAL(updated()), this, SIGNAL(updated()));

    myScene = new GameScene(myGame, this);
    connect(myGame, SIGNAL(updated()), this, SIGNAL(updated()));

    //myGame = new GameServer(myKeyManager, this);
    myKeyManager->addReleaseEvent(Qt::Key_Escape, this, SLOT(close()));
    myKeyManager->addReleaseEvent(Qt::Key_Space, this, SLOT(refresh()));
    myKeyManager->addReleaseEvent(Qt::Key_P, this, SLOT(pause()));
    myGame->play();
}

void GameState::added() {

}

void GameState::returned() {

}

void GameState::deleted() {

}

void GameState::covered() {

}

Game *GameState::getGame() {
    return myGame;
}

PaintScene *GameState::getScene() const {
    return myScene;
}

void GameState::refresh() {
    if (myGame->getState() == "game over") {
        myGame->refresh();
    } else if (myGame->getState() == "paused") {
        myGame->play();
    }
}

void GameState::pause() {
    myGame->pause();
}

void GameState::close() {
    //GameState *state = new GameState(myKeyManager, this);
    State *state = myStateManager->popState();
    state->deleteLater();
}
